from public.utils import get_web_entrance
from public.message import web_verification_msg
from robot.botClient import get_bot_client
from robot.comm.pluginBase import Session
from robot.comm.command import NormalCommand, FullCommand
from alicebot.adapter.mirai.event import FriendMessage
from urllib.parse import urljoin
import random


@NormalCommand('网页版')
async def web_entrance(session: Session, name: str):
    base = get_web_entrance()
    if not base:
        await session.reply('未找到网址，natapp可能未启动')
        return

    url = str(urljoin(base, {
        '查询金币': '/inquiry',
        '井字棋': '/tic_tac_toe',
        '扫雷': '/minesweeper',
        '24点': '/p24',
        '见缝插针': '/stick_pin',
        '2048': '/mei_vue/#/2048',
        '俄罗斯方块': '/mei_vue/#/Tetris',
    }.get(name)))

    if isinstance(session.event, FriendMessage):
        qq = str(session.qq)
        code = str(random.randint(1000, 9999))
        url += f'?qq={qq}&code={code}'
        if get_bot_client().send(web_verification_msg.build(qq=qq, code=code)):
            await session.reply(f'链接：{url}\n此链接为个人私有，请勿分享给他人')
            return
    await session.reply(f'链接：{url}')


@FullCommand('井字棋')
async def tic_tac_toe_entrance(session: Session):
    await web_entrance(session, '井字棋')


@FullCommand('扫雷')
async def minesweeper(session: Session):
    await web_entrance(session, '扫雷')


@FullCommand('见缝插针')
async def stick_pin(session: Session):
    await web_entrance(session, '见缝插针')


@FullCommand('俄罗斯方块')
async def tetris(session: Session):
    await web_entrance(session, '俄罗斯方块')
